﻿using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using APP.HELPER.Data.Helper;
using APP.HELPER.Utils.CustomizeToolbox;
using DevExpress.XtraEditors;
using ComboBox = System.Windows.Forms.ComboBox;

namespace Dictionary.ERP
{
    public class HRM_PRODUCT
    {
        private string m_Barcode;
        private string m_Description;
        private string m_NameProduct;
        private Image m_Photo;
        private decimal m_Price;
        private string m_ProductCode;
        private string m_ProductGroupCode;
        private string m_Unit;

        public HRM_PRODUCT()
        {
            m_ProductCode = "";
            m_ProductGroupCode = "";
            m_NameProduct = "";
            m_Unit = "";
            m_Photo = null;
            m_Barcode = "";
            m_Price = 0M;
            m_Description = "";
        }

        public HRM_PRODUCT(string ProductCode, string ProductGroupCode, string NameProduct, string Unit, string Barcode,
            decimal Price, string Description)
        {
            m_ProductCode = ProductCode;
            m_ProductGroupCode = ProductGroupCode;
            m_NameProduct = NameProduct;
            m_Unit = Unit;
            m_Barcode = Barcode;
            m_Price = Price;
            m_Description = Description;
        }

        [Category("Column"), DisplayName("Barcode")]
        public string Barcode
        {
            get { return m_Barcode; }
            set { m_Barcode = value; }
        }

        [Category("Column"), DisplayName("Description")]
        public string Description
        {
            get { return m_Description; }
            set { m_Description = value; }
        }

        [DisplayName("NameProduct"), Category("Column")]
        public string NameProduct
        {
            get { return m_NameProduct; }
            set { m_NameProduct = value; }
        }

        [Category("Column"), DisplayName("Photo")]
        public Image Photo
        {
            get { return m_Photo; }
            set { m_Photo = value; }
        }

        [DisplayName("Price"), Category("Column")]
        public decimal Price
        {
            get { return m_Price; }
            set { m_Price = value; }
        }

        [Category("Primary Key"), DisplayName("ProductCode")]
        public string ProductCode
        {
            get { return m_ProductCode; }
            set { m_ProductCode = value; }
        }

        [DisplayName("ProductGroupCode"), Category("Column")]
        public string ProductGroupCode
        {
            get { return m_ProductGroupCode; }
            set { m_ProductGroupCode = value; }
        }

        public string ProductName
        {
            get { return "Class HRM_PRODUCT"; }
        }

        public string ProductVersion
        {
            get { return "1.0.0.0"; }
        }

        [DisplayName("Unit"), Category("Column")]
        public string Unit
        {
            get { return m_Unit; }
            set { m_Unit = value; }
        }

        public void AddCombo(ComboBox combo)
        {
            GridCalendarControlLib.TableToComboBox(combo, GetList(), "NameProduct", "ProductCode");
        }

        public void AddComboAll(ComboBox combo)
        {
            var list = new DataTable();
            list = GetList();
            DataRow row = list.NewRow();
            row["ProductCode"] = "All";
            row["NameProduct"] = "Tất cả";
            list.Rows.InsertAt(row, 0);
            GridCalendarControlLib.TableToComboBox(combo, list, "NameProduct", "ProductCode");
        }

        public void AddComboEdit(ComboBoxEdit combo)
        {
            var list = new DataTable();
            list = GetList();
            foreach (DataRow row in list.Rows)
            {
                combo.Properties.Items.Add(row["NameProduct"]);
            }
        }

        public string Delete()
        {
            string[] myParams = {"@ProductCode"};
            object[] myValues = {m_ProductCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_PRODUCT_Delete", myParams, myValues);
        }

        public string Delete(string ProductCode)
        {
            string[] myParams = {"@ProductCode"};
            object[] myValues = {ProductCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_PRODUCT_Delete", myParams, myValues);
        }

        public string Delete(SqlConnection myConnection, string ProductCode)
        {
            string[] myParams = {"@ProductCode"};
            object[] myValues = {ProductCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "HRM_PRODUCT_Delete", myParams, myValues);
        }

        public string Delete(SqlTransaction myTransaction, string ProductCode)
        {
            string[] myParams = {"@ProductCode"};
            object[] myValues = {ProductCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_PRODUCT_Delete", myParams, myValues);
        }

        public bool Exist(string ProductCode)
        {
            bool hasRows = false;
            string[] myParams = {"@ProductCode"};
            object[] myValues = {ProductCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("HRM_PRODUCT_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                helper.Close();
                reader = null;
            }
            return hasRows;
        }

        public string Get(string ProductCode)
        {
            string str = "";
            string[] myParams = {"@ProductCode"};
            object[] myValues = {ProductCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("HRM_PRODUCT_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_ProductCode = Convert.ToString(reader["ProductCode"]);
                    m_ProductGroupCode = Convert.ToString(reader["ProductGroupCode"]);
                    m_NameProduct = Convert.ToString(reader["NameProduct"]);
                    m_Unit = Convert.ToString(reader["Unit"]);
                    m_Barcode = Convert.ToString(reader["Barcode"]);
                    m_Price = Convert.ToDecimal(reader["Price"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    if (!reader.IsDBNull(reader.GetOrdinal("Photo")) && (reader["Photo"] != Convert.DBNull))
                    {
                        var buffer = (byte[]) reader["Photo"];
                        if (buffer.Length != 2)
                        {
                            var stream = new MemoryStream(buffer);
                            m_Photo = Image.FromStream(stream);
                        }
                        if (buffer.Length == 2)
                        {
                            m_Photo = null;
                        }
                    }
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlConnection myConnection, string ProductCode)
        {
            string str = "";
            string[] myParams = {"@ProductCode"};
            object[] myValues = {ProductCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myConnection, "HRM_PRODUCT_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_ProductCode = Convert.ToString(reader["ProductCode"]);
                    m_ProductGroupCode = Convert.ToString(reader["ProductGroupCode"]);
                    m_NameProduct = Convert.ToString(reader["NameProduct"]);
                    m_Unit = Convert.ToString(reader["Unit"]);
                    m_Barcode = Convert.ToString(reader["Barcode"]);
                    m_Price = Convert.ToDecimal(reader["Price"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    if (!reader.IsDBNull(reader.GetOrdinal("Photo")) && (reader["Photo"] != Convert.DBNull))
                    {
                        var buffer = (byte[]) reader["Photo"];
                        if (buffer.Length != 2)
                        {
                            var stream = new MemoryStream(buffer);
                            m_Photo = Image.FromStream(stream);
                        }
                        if (buffer.Length == 2)
                        {
                            m_Photo = null;
                        }
                    }
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlTransaction myTransaction, string ProductCode)
        {
            string str = "";
            string[] myParams = {"@ProductCode"};
            object[] myValues = {ProductCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myTransaction, "HRM_PRODUCT_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_ProductCode = Convert.ToString(reader["ProductCode"]);
                    m_ProductGroupCode = Convert.ToString(reader["ProductGroupCode"]);
                    m_NameProduct = Convert.ToString(reader["NameProduct"]);
                    m_Unit = Convert.ToString(reader["Unit"]);
                    m_Barcode = Convert.ToString(reader["Barcode"]);
                    m_Price = Convert.ToDecimal(reader["Price"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    if (!reader.IsDBNull(reader.GetOrdinal("Photo")) && (reader["Photo"] != Convert.DBNull))
                    {
                        var buffer = (byte[]) reader["Photo"];
                        if (buffer.Length != 2)
                        {
                            var stream = new MemoryStream(buffer);
                            m_Photo = Image.FromStream(stream);
                        }
                        if (buffer.Length == 2)
                        {
                            m_Photo = null;
                        }
                    }
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public DataTable GetList()
        {
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("HRM_PRODUCT_GetList");
        }

        public string Insert()
        {
            string[] myParams =
            {
                "@ProductCode", "@ProductGroupCode", "@NameProduct", "@Unit", "@Barcode", "@Price",
                "@Description"
            };
            object[] myValues =
            {
                m_ProductCode, m_ProductGroupCode, m_NameProduct, m_Unit, m_Barcode, m_Price,
                m_Description
            };
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_PRODUCT_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction)
        {
            string[] myParams =
            {
                "@ProductCode", "@ProductGroupCode", "@NameProduct", "@Unit", "@Barcode", "@Price",
                "@Description"
            };
            object[] myValues =
            {
                m_ProductCode, m_ProductGroupCode, m_NameProduct, m_Unit, m_Barcode, m_Price,
                m_Description
            };
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_PRODUCT_Insert", myParams, myValues);
        }

        public string Insert(string ProductCode, string ProductGroupCode, string NameProduct, string Unit,
            string Barcode, decimal Price, string Description)
        {
            string[] myParams =
            {
                "@ProductCode", "@ProductGroupCode", "@NameProduct", "@Unit", "@Barcode", "@Price",
                "@Description"
            };
            object[] myValues = {ProductCode, ProductGroupCode, NameProduct, Unit, Barcode, Price, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_PRODUCT_Insert", myParams, myValues);
        }

        public string Insert(SqlConnection myConnection, string ProductCode, string ProductGroupCode, string NameProduct,
            string Unit, string Barcode, decimal Price, string Description)
        {
            string[] myParams =
            {
                "@ProductCode", "@ProductGroupCode", "@NameProduct", "@Unit", "@Barcode", "@Price",
                "@Description"
            };
            object[] myValues = {ProductCode, ProductGroupCode, NameProduct, Unit, Barcode, Price, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "HRM_PRODUCT_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction, string ProductCode, string ProductGroupCode,
            string NameProduct, string Unit, string Barcode, decimal Price, string Description)
        {
            string[] myParams =
            {
                "@ProductCode", "@ProductGroupCode", "@NameProduct", "@Unit", "@Barcode", "@Price",
                "@Description"
            };
            object[] myValues = {ProductCode, ProductGroupCode, NameProduct, Unit, Barcode, Price, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_PRODUCT_Insert", myParams, myValues);
        }

        public string NewID()
        {
            return SqlHelper.GenCode("HRM_PRODUCT", "ProductCode", "HH");
        }

        public string Update()
        {
            string[] myParams =
            {
                "@ProductCode", "@ProductGroupCode", "@NameProduct", "@Unit", "@Barcode", "@Price",
                "@Description"
            };
            object[] myValues =
            {
                m_ProductCode, m_ProductGroupCode, m_NameProduct, m_Unit, m_Barcode, m_Price,
                m_Description
            };
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_PRODUCT_Update", myParams, myValues);
        }

        public string Update(string ProductCode, Image photo)
        {
            byte[] buffer;
            var helper = new SqlHelper
            {
                CommandType = CommandType.Text
            };
            if (photo != null)
            {
                var stream = new MemoryStream();
                photo.Save(stream, photo.RawFormat);
                buffer = stream.GetBuffer();
                stream.Close();
            }
            else
            {
                buffer = new byte[2];
            }
            string[] myParams = {"@ProductCode", "@Photo"};
            object[] myValues = {ProductCode, buffer};
            return helper.ExecuteNonQuery("Update HRM_PRODUCT set Photo=@Photo WHERE ProductCode=@ProductCode", myParams,
                myValues);
        }

        public string Update(string ProductCode, string ProductGroupCode, string NameProduct, string Unit,
            string Barcode, decimal Price, string Description)
        {
            string[] myParams =
            {
                "@ProductCode", "@ProductGroupCode", "@NameProduct", "@Unit", "@Barcode", "@Price",
                "@Description"
            };
            object[] myValues = {ProductCode, ProductGroupCode, NameProduct, Unit, Barcode, Price, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("HRM_PRODUCT_Update", myParams, myValues);
        }

        public string Update(SqlConnection myConnection, string ProductCode, string ProductGroupCode, string NameProduct,
            string Unit, string Barcode, decimal Price, string Description)
        {
            string[] myParams =
            {
                "@ProductCode", "@ProductGroupCode", "@NameProduct", "@Unit", "@Barcode", "@Price",
                "@Description"
            };
            object[] myValues = {ProductCode, ProductGroupCode, NameProduct, Unit, Barcode, Price, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "HRM_PRODUCT_Update", myParams, myValues);
        }

        public string Update(SqlTransaction myTransaction, string ProductCode, string ProductGroupCode,
            string NameProduct, string Unit, string Barcode, decimal Price, string Description)
        {
            string[] myParams =
            {
                "@ProductCode", "@ProductGroupCode", "@NameProduct", "@Unit", "@Barcode", "@Price",
                "@Description"
            };
            object[] myValues = {ProductCode, ProductGroupCode, NameProduct, Unit, Barcode, Price, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "HRM_PRODUCT_Update", myParams, myValues);
        }
    }
}